rm(list=ls(all=TRUE))
library(sf)
library(readstata13)
library(tidyverse)
library(gridExtra)
library(grid)

setwd("~/replication") # makes sure the directory contains the folder "worldmap"

world <- st_read("worldmap")
world <- world[!world$country2=="Antarctica",]

data <- read.dta13("news_fav.dta",convert.factors = F)
data <- data%>%
  select(year,sentiment,ccodealp)

plot_list <- list()
for (j in 1:16){
  i<-2002+j
  datayear<-subset(data,year==i,c(ccodealp,sentiment))
  datayear$sentiment <- cut(datayear$sentiment, breaks = c(-5.1,-4,-3,-2,-1,0,1,2,3,4,5),
                         labels=c('[-5,-4]','(-4,-3]','(-3,-2]','(-2,-1]','(-1,0]','(0,1]','(1,2]','(2,3]','(3,4]','(4,5]'))
  rownames(datayear) <- NULL
  datayear<-na.omit(datayear)
  worlddata <- left_join(world, datayear, by = c("adm0_a3_us"="ccodealp"))

  plot_list[[j]]<-ggplot() +geom_sf(data = worlddata,aes(fill = sentiment),colour = "black",size=0.1)+
    geom_sf(data = worlddata,aes(fill = sentiment),colour = "black",size=0.1)+
    ggtitle(i)+ labs(x=NULL, y=NULL)+
    coord_sf(crs = 4326)+
    scale_fill_manual(name="",values=c('#67001f','#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac','#053061'), na.value = "white")+
    guides(fill = guide_legend(nrow = 1))+
    theme(legend.position="bottom",
          panel.grid = element_blank(),
          panel.background = element_rect(fill = "white"),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          axis.title = element_blank(),
          plot.title = element_text(size = 12, hjust = 0.5),
          plot.margin = margin(t = 0,r = 0,b = 0,l = 0))
}

grid_arrange_shared_legend <- function(..., nrow = 1, ncol = length(list(...)), position = c("bottom", "right")) {
  
  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[16]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position = "none"))
  gl <- c(gl, nrow = nrow, ncol = ncol)
  
  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  grid.newpage()
  grid.draw(combined)
}

ppi <- 600
png("fig3.png",width=10*ppi, height=6*ppi, res=ppi)
grid_arrange_shared_legend(plot_list[[1]],plot_list[[2]],plot_list[[3]],
                           plot_list[[4]],plot_list[[5]],plot_list[[6]],
                           plot_list[[7]],plot_list[[8]],plot_list[[9]],
                           plot_list[[10]],plot_list[[11]],plot_list[[12]],
                           plot_list[[13]],plot_list[[14]],plot_list[[15]],
                           plot_list[[16]],nrow = 4, ncol = 4)
dev.off()